﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MOJsdk;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Threading;

namespace MoJMoviePanels
{
    /// <summary>
    /// Interaction logic for ManageMoviesControl.xaml
    /// </summary>
    public partial class ManageMoviesControl : UserControl, IPanel
    {
        private ObservableCollection<MojMovie> _movies;
        private ObservableCollection<MojMovie> _view;
        private IMetaData _details;

        public ManageMoviesControl()
        {
            InitializeComponent();

            _Grid.ApplyStyle("MojMoviePanels_ManageMovies_Grid_Background");
            _gridWaiting.ApplyStyle("MojMoviePanels_ManageMovies_Grid_Waiting");
            _listView.ApplyStyle("MojMoviePanels_ManageMovies_listView");
            _ProgBar.ApplyStyle("MojMoviePanels_ManageMovies_ProgressBar");

            _gridWaiting.Visibility = System.Windows.Visibility.Hidden;

            _movies = new ObservableCollection<MojMovie>();
            _view = new ObservableCollection<MojMovie>();

            _details = MojStatusVarsEnum.MetaDataControl.Value();
            _grid_details.Children.Add(_details.Control);

            INIEvents();
        }
        private void INIEvents()
        {
            OnSelectedFileChanged += delegate { };
            OnSelectedMovieChanged += delegate { };
            OnLoadComplete += delegate { };

            this.Loaded += delegate { LoadMovies(); };

            _listView.SelectionChanged += OnSelectionChange;
        }

        #region Ipanel
        public UserControl Control { get { return this; } }
        public string PanelIdentifier { get { return UserVars.ManageMoviesPanel; } }
        public string PanelType { get { return MojIPanelStandardTypes.ManageMovies; } }

        public void LoadMovies()
        {
            NoArgDelegate th = new NoArgDelegate(LoadMoviesAsync);
            th.BeginInvoke(null, null);
        }

        public void Focus()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                _listView.Focus();
                SelectIndex(0);
            });
        }
        public void SelectNext()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                int x = _listView.Items.Count - 1;
                if (x == -1) return;

                int select = _listView.SelectedIndex - 1;
                if (select == -1) select = x;

                _listView.SelectedIndex = select;
            });
        }
        public void SelectPrev() 
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                int x = _listView.Items.Count - 1;
                if (x == -1) return;

                int select = _listView.SelectedIndex - 1;
                if (select == -1) select = x;

                _listView.SelectedIndex = select;
            });
        }
        public void SelectIndex(int Index)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                int max = _listView.Items.Count-1;
                if (Index > max) return;

                _listView.SelectedIndex = Index;
                _listView.ScrollIntoView(_listView.SelectedItem);
            });
        }

        public void SelectMovie(string MovieCode) { }
        public void SelectMovies(List<string> MovieCodes) { }

        public void SelectFile(int FileId) { }
        public void SelectFiles(List<int> FileIds) { }

        public int CollectionSize { get { return _view.Count; } }
        public bool IsMultiSelect { get { return false; } }

        public string SelectedMovie { get { return "NULL"; } }
        public int SelectedFile { get { return -1; } }

        public List<string> SelectedMovies { get { return new List<string>(); } }
        public List<int> SelectedFiles { get { return new List<int>(); } }

        public void ApplyMovieFilter() { }
        public void ApplyMovieOrder() { }

        public void ApplyFileFilter() { }
        public void ApplyFileOrder() { }

        public event SelectedMovieChangedHandler OnSelectedMovieChanged;
        public event SelectedFileChangedHandler OnSelectedFileChanged;
        public event CallEventHandler OnLoadComplete;

        public bool Equals(IPanel Panel)
        {
            return this.PanelIdentifier == Panel.PanelIdentifier;
        }
        #endregion

        private void SetWaitingStatus(bool Value)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                _gridWaiting.Visibility = Value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
                _listView.IsEnabled = !Value;
            });
        }
        private void SetProgressBar(double Value, double Max)
        {
            DispatcherOperation op = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                _ProgBar.Maximum = Max;
                _ProgBar.Value = Value;
            });
            while (op.Status == DispatcherOperationStatus.Executing) { }
        }
        private void ResetCollection()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                _movies = new ObservableCollection<MojMovie>();
                _view = new ObservableCollection<MojMovie>();
                _listView.ItemsSource = _view;
            });
        }

        private void LoadMoviesAsync()
        {
            SetWaitingStatus(true);
            ResetCollection();

            int count = 0;
            int max = MojHost.GetHost.MoJSystem.MoviesInterop.Movies.Count;

            foreach (var v in MojHost.GetHost.MoJSystem.MoviesInterop.Movies)
            {
                count++;
                MojMovie m = new MojMovie(v);
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
                {
                    _movies.Add(m);
                    _view.Add(m);
                });
                SetProgressBar(count, max);
            }

            SetWaitingStatus(false);
        }

        private void OnSelectionChange(object sender, RoutedEventArgs e)
        {
            int x = _listView.SelectedIndex;
            if (x == -1) return;

            _details.LoadMovie(_view[x].MovieCode);
        }
    }
}
